﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using eschoolBO;

namespace ESCHOOL.admin
{
    public partial class WebForm26 : System.Web.UI.Page
    {
        letter lt = new letter();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnupdate.Visible = false;
                DataTable dt = lt.GetRecords(-99, "", "", false, " and ");
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }

        protected void btninsert_Click(object sender, EventArgs e)
        {
            lt.n_id = -99;
            lt.n_name = tbname.Text;
            if (FileUpload1.HasFile)
                           FileUpload1.PostedFile.SaveAs(Server.MapPath("~/admin/newsletter/" + FileUpload1.FileName));
                lt.n_letter = FileUpload1.FileName;
                lt.isdeleted = false;
                lt.SaveRecords();
                Response.Redirect("~/admin/Newsletter.aspx");
        }
        protected void btnedit_Command(object sender, CommandEventArgs e)
        {
            btnupdate.Visible = true;
            btninsert.Visible = false;
            DataTable dt = lt.GetRecords(Convert.ToInt64(e.CommandArgument), "", "", false, " and ");
            tbid.Text = dt.Rows[0]["n_id"].ToString();
            tbname.Text = dt.Rows[0]["n_name"].ToString();
            tbletter.Text = dt.Rows[0]["n_letter"].ToString();            
        }

        protected void btndelete_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt = lt.GetRecords(Convert.ToInt64(e.CommandArgument), "", "", false, " and ");
                lt.n_id=Convert.ToInt64(dt.Rows[0]["n_id"].ToString());
                lt.n_name = dt.Rows[0]["n_name"].ToString();
                lt.n_letter = dt.Rows[0]["n_letter"].ToString();
                lt.isdeleted = true;
                lt.SaveRecords();
                Response.Redirect("~/admin/Newsletter.aspx");
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void btnupdate_Click(object sender, EventArgs e)
        {
            lt.n_id = Convert.ToInt64(tbid.Text);
            lt.n_name = tbname.Text;
            if (FileUpload1.HasFile)
            {
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/admin/newsletter/" + FileUpload1.FileName));
                lt.n_letter = FileUpload1.FileName;
            }
            else
                lt.n_letter = tbletter.Text;
            lt.isdeleted = false;
            lt.SaveRecords();
            Response.Redirect("~/admin/Newsletter.aspx");
        }
    }
}
